/////////////////////////////////////////////
// ObjectARX defined commands

#include "StdAfx.h"
#include "StdArx.h"


// This is command 'WINDO'
void windo()
{
	AcDbBlockTableRecord *pBlkTableRecord;

	RectWindow *pRectWindow;	// RectWindow class

	pRectWindow = new RectWindow;

	
	if(!getWindowInputParameters(pRectWindow))
	{
		acutPrintf("\nError in window input parameters. ");
		if(pRectWindow != NULL)
		{
			delete pRectWindow;
		}
		return;
	}

	if(!getModelSpaceRecord(pBlkTableRecord))
	{
		return;
	}

	pRectWindow->drawWindow(pBlkTableRecord);

	pBlkTableRecord->close();

	// Here is where we create our extension dictionary
	pRectWindow->addXtnDict();

	// Here is where we extract data out of our dictionary
	pRectWindow->printXtnDict();

	if(pRectWindow != NULL)
	{ 
		delete pRectWindow;
	}
}

// This is command 'PWI'
void pickWindowInfo()
{
	AcDbDatabase *pCurDb;
	AcDbObject *pObj;
	AcDbObjectId windoId;
	AcDbHandle handEnt;
	AcDbDictionary *pNamedObjD, *pDict;
	AcDbXrecord *pXrec;

	struct resbuf *prbXtnDict;
	struct resbuf *pTemp;

	ads_name ename;
	ads_point pickpt;

	char strHandle[17];

	int rc;

	pCurDb = acdbHostApplicationServices()->workingDatabase();

    pCurDb->getNamedObjectsDictionary(pNamedObjD, AcDb::kForRead);
	if(!pNamedObjD->has("WINDO_INFO"))
	{
		acutPrintf("\nNo WINDO_INFO key in Named Objects Dictionary ");
		pNamedObjD->close();
		return;
	}

	rc = acedEntSel("\nSelecy Window enity ", ename, pickpt);
	if(rc != RTNORM)
	{
		acutPrintf("\nError selecting entity ");
		return;
	}

	// Get the entity object ID
	acdbGetObjectId(windoId, ename);

	// Open the enity for a read operation
	acdbOpenObject(pObj, windoId, AcDb::kForRead);

	pObj->getAcDbHandle(handEnt);

	handEnt.getIntoAsciiBuffer(strHandle);

    pObj->close();

    pNamedObjD->getAt("WINDO_INFO", (AcDbObject*&)pDict, AcDb::kForRead);
    pNamedObjD->close();

    pDict->getAt(strHandle, (AcDbObject*&) pXrec, AcDb::kForRead);
    pDict->close();

    // get the Xrecord's data list then close the Xrecord
    pXrec->rbChain(&prbXtnDict);
    pXrec->close();

	// Print out the extension dictionary data
	// Here I know the order of the  extension dictionary data
	pTemp = prbXtnDict;

	acutPrintf("\nWindow Type = %s", pTemp->resval.rstring);

	pTemp = pTemp->rbnext;
	acutPrintf("\nRows = %d", pTemp->resval.rint);

	pTemp = pTemp->rbnext;
	acutPrintf("\nCols = %d", pTemp->resval.rint);

	pTemp = pTemp->rbnext;
	acutPrintf("\nLength = %.2lf", pTemp->resval.rreal);

	pTemp = pTemp->rbnext;
	acutPrintf("\nHeight = %.2lf", pTemp->resval.rreal);

	pTemp = pTemp->rbnext;
	acutPrintf("\nStart Point = (%.2lf, %.2lf)", pTemp->resval.rpoint[X], pTemp->resval.rpoint[Y]);
	
    acutRelRb(prbXtnDict);
	
}